package com.googlecode.jmicroerp.wicket.administrare;

import com.googlecode.jmicroerp.model.Magazin;
import com.googlecode.jmicroerp.model.Rol;
import com.googlecode.jmicroerp.model.Utilizator;
import com.googlecode.jmicroerp.repository.GenericRestriction;
import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.service.UtilizatorService;
import com.googlecode.jmicroerp.wicket.DefaultEditPage;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.PatternValidator;

/**
 *
 * @author Alexandru.Codescu
 */
public class UtilizatoriEditPage extends DefaultEditPage {

    private static final long serialVersionUID = 1L;
    private Utilizator utilizator;
    @SpringBean(name = "utilizatorService")
    private UtilizatorService utilizatorService;
    @SpringBean(name = "genericService")
    private GenericService<Rol> rolService;
    @SpringBean(name = "genericService")
    private GenericService<Magazin> magazinService;
    private Form<Utilizator> form;
    private String parola;
    // Flag care indica daca utilizatorul este adaugat sau exista deja
    private Boolean adaugat = false;
    private PasswordTextField pssPassword;

    public UtilizatoriEditPage(Long idClient) {
        utilizator = utilizatorService.getById(idClient);
        parola = utilizator.getParola();
        initPage();
    }

    public UtilizatoriEditPage() {
        utilizator = new Utilizator();
        GenericRestriction res = new GenericRestriction("denumire", "ROLE_USER");
        Rol rolUser = rolService.getSingleResult(Rol.class, res);
        
        utilizator.setRoluri(new ArrayList<Rol>());
        utilizator.getRoluri().add(rolUser);
        // Utilizator nou
        adaugat = true;
        initPage();
    }

    private void initPage() {
        form = new Form<Utilizator>("form");
        form.setModel(new CompoundPropertyModel<Utilizator>(utilizator));
        
        TextField<String> txtUsername = new TextField<String>("numeUtilizator");
        txtUsername.setRequired(true);
        Label lblUsername = new Label("lbl_username", UtilizatoriEditPage.this.getString("lbl_username"));
        form.add(lblUsername);
        form.add(txtUsername);

        pssPassword = new PasswordTextField("parola");
        if (adaugat) {
            pssPassword.setRequired(true);
            pssPassword.modelChanged();
        } else {
            pssPassword.setRequired(false);
            pssPassword.modelChanged();
        }
        pssPassword.add(new PatternValidator(Utilizator.PASSWORD_PATTERN));
        pssPassword.setResetPassword(true);
        Label lblPassword = new Label("lbl_parola", UtilizatoriEditPage.this.getString("lbl_parola"));
        form.add(lblPassword);
        form.add(pssPassword);
        
        DropDownChoice<Magazin> ddcMagazin = new DropDownChoice<Magazin>("magazin", new LoadableDetachableModel<List<Magazin>>() {

            @Override
            protected List<Magazin> load() {
                return magazinService.getList(Magazin.class, null, null);
            }
        }, new ChoiceRenderer<Magazin>("denumire", "id"));
        form.add(ddcMagazin);

        ListMultipleChoice<Rol> lmcRoluri = new ListMultipleChoice<Rol>(
                "roluri", new LoadableDetachableModel<List<Rol>>() {
            private static final long serialVersionUID = 1L;

            @Override
            protected List<Rol> load() {
                return rolService.getList(Rol.class, 0, Integer.MAX_VALUE, null, null);
            }
        }, new ChoiceRenderer<Rol>("denumire", "id"));
        Label lblRoluri = new Label("lbl_roluri", UtilizatoriEditPage.this.getString("lbl_roluri"));
        form.add(lblRoluri);
        form.add(lmcRoluri);

        SubmitLink salvare = new SubmitLink("salvare") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onSubmit() {

                /*Asta nu e bine -- trebuie modificata; Ar trebui sa verificam si la modificare ca nu exista duplicate.
                 ATENTIE: nu scoate conditia "adaugat = true" , fa un query care compare usernameul introdus cu toate celelalte
                 din baza mai putin cel pe care ai dat modificare*/

                //daca suntem pe modificare si nici nu a completat nimic in campula de parola ii lasam parola nemodificata
                if (!adaugat && pssPassword.getModelObject() == null) {
                    utilizator.setParola(parola);
                    utilizatorService.save(utilizator, false);
                } else {
                    utilizatorService.save(utilizator, true);
                }
                setResponsePage(UtilizatoriViewPage.class);
            }
        };

        Link renuntare = new Link("renuntare") {
            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                setResponsePage(UtilizatoriViewPage.class);
            }
        };

        form.add(salvare);
        form.add(renuntare);

        FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
        form.add(feedbackPanel);

        add(form);
    }
}
